const db = require('../db/index.js')
// 导入加密中间件
const bcrypt = require('bcryptjs')
// 导入nodejs的crypto生成onlyid
const crypto = require('crypto')
// 处理文件路径
fs = require('fs')



//上传商品
exports.uploadGoods = (req, res) => {
	const reqinfo = req.body
	console.log(reqinfo, 666);
	const sql = 'insert into goods set ?'
	db.query(sql, {
		goods_name: reqinfo.goods_name,
		goods_price: reqinfo.goods_price,
		goods_category: reqinfo.goods_category,
		image_url: reqinfo.image_url,
		goods_stock: reqinfo.goods_stock,
		goods_status: reqinfo.goods_status,
		goods_process: reqinfo.goods_process,
		goods_describe: reqinfo.goods_describe
	}, (err, result) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message: '上传商品成功',
		})
	})
}

// 修改商品
exports.modifyGoods = (req, res) => {
    const {
        goods_id,
        goods_name,
        goods_price,
        goods_category,
        image_url,
        goods_stock,
        goods_status,
        goods_process,
        goods_describe
    } = req.body;
	console.log(req.body);
    const sql =
        'UPDATE goods SET goods_name=?, goods_price=?, goods_category=?, image_url=?, goods_stock=?, goods_status=?, goods_process=?, goods_describe=? WHERE goods_id=?';

    db.query(sql, [goods_name, goods_price, goods_category, image_url, goods_stock, goods_status, goods_process, goods_describe, goods_id], (err, result) => {
        if (err) return res.cc(err);
        res.send({
            status: 0,
            message: '修改商品成功',
        });
    });
};




// 获取商品信息 接收参数 goods_id
exports.getGoodsInfo = (req, res) => {
	const sql = 'select * from goods where goods_id = ?'
	db.query(sql, req.body.goods_id, (err, result) => {
		if (err) return res.cc(err)
		res.send(result[0])
	})
}

// 获取所有商品信息
exports.getAllGoodsInfo = (req, res) => {
	const sql = 'SELECT * FROM goods';
	db.query(sql, (err, result) => {
		if (err) {
			console.error('查询商品信息失败:', err);
			return res.status(500).json({
				message: '查询商品信息失败'
			});
		}
		// 返回查询到的所有商品信息
		res.status(200).json(result);
	});
};


// 上传商品图
exports.uploadGoodsAvatar = (req, res) => {
	let oldName = req.files[0].filename;
	let newName = Buffer.from(req.files[0].originalname, 'latin1').toString('utf8')
	fs.renameSync('./public/upload/' + oldName, './public/upload/' + newName)
	const sql = 'insert into goodsimg set ?'
	db.query(sql, {
		image_url: `http://127.0.0.1:3007/upload/${newName}`,
	}, (err, result) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			url: 'http://127.0.0.1:3007/upload/' + newName,
			message: '上传商品图片成功',
		})
	})
}

// 删除商品信息
exports.deleteGoodsInfo = (req, res) => {
	const {
		goods_id
	} = req.body;

	if (!goods_id) {
		return res.status(400).json({
			message: '商品ID不能为空'
		});
	}

	const sql = 'DELETE FROM goods WHERE goods_id = ?';
	db.query(sql, goods_id, (err, result) => {
		if (err) {
			console.error('删除商品信息失败:', err);
			return res.status(500).json({
				message: '删除商品信息失败'
			});
		}

		if (result.affectedRows === 0) {
			return res.status(404).json({
				message: '未找到对应的商品信息'
			});
		}

		res.status(200).json({
			message: '删除商品信息成功'
		});
	});
};


